using Microsoft.AspNetCore.Mvc.Filters;

namespace Read.Utility
{
    /// <summary>
    /// 异常时执行
    /// throw new Exception("Test Exception");
    /// http://www.bubuko.com/infodetail-1971338.html
    /// http://www.binaryintellect.net/articles/5df6e275-1148-45a1-a8b3-0ba2c7c9cea1.aspx
    /// </summary>
    public class HandleExceptionAttribute : ExceptionFilterAttribute
    {
        /// <summary>
        /// OnException
        /// </summary>
        /// <param name="context"></param>
        public override void OnException(ExceptionContext context)
        {
            // var result = new ViewResult { ViewName = "Error" };
            // var modelMetadata = new EmptyModelMetadataProvider();
            // result.ViewData = new ViewDataDictionary(modelMetadata, context.ModelState);
            // result.ViewData.Add("HandleException", context.Exception);
            // context.Result = result;
            // context.ExceptionHandled = true;
            string host = context.HttpContext.Request.Host.ToString();
            string path = context.HttpContext.Request.Path.ToString();
            string queryString = context.HttpContext.Request.QueryString.ToString();
            Logs log = new Logs(context.Exception, host, path, queryString);
            log.WriteFile();
        }
    }
}